package com.atguigu.gmall.gmallsuger.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.gmallsuger.bean.Product;
import com.atguigu.gmall.gmallsuger.service.KeywordStatsService;
import com.atguigu.gmall.gmallsuger.service.ProductStatsService;
import com.atguigu.gmall.gmallsuger.service.ProvinceStatsService;
import com.atguigu.gmall.gmallsuger.service.VisitorStatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/9/4 10:05
 */
@RestController
public class SugarController {
    
    @Autowired
    ProductStatsService product;
    
    @RequestMapping("/sugar/api/gmv")
    public String gmv(@RequestParam(value = "date", defaultValue = "0") int date) { // 如果date不传, 则date=0
        date = defaultDate(date);
        
        BigDecimal gmv = product.gmv(date);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        result.put("data", gmv);
        
        return result.toJSONString();
    }
    
    @RequestMapping("/sugar/api/gmv/tm")
    public String gmvByTm(
        @RequestParam(value = "date", defaultValue = "0") int date,
        @RequestParam(value = "limit", defaultValue = "5") int limit) { // 如果date不传, 则date=0
        date = defaultDate(date);
        
        List<Product> list = product.gmvByTm(date, limit);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray categories = new JSONArray();
        for (Product p : list) {
            categories.add(p.getName());
        }
        
        data.put("categories", categories);
        
        JSONArray series = new JSONArray();
        
        JSONObject data1 = new JSONObject();
        data1.put("name", "商品品牌");
        JSONArray d = new JSONArray();
        for (Product p : list) {
            d.add(p.getOrder_amount());
        }
        data1.put("data", d);
        
        series.add(data1);
        
        data.put("series", series);
        
        result.put("data", data);
        
        return result.toJSONString();
    }
    
    @RequestMapping("/sugar/api/gmv/c3")
    public String gmvByC3(
        @RequestParam(value = "date", defaultValue = "0") int date,
        @RequestParam(value = "limit", defaultValue = "5") int limit) { // 如果date不传, 则date=0
        date = defaultDate(date);
        
        List<Product> list = product.gmvByC3(date, limit);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONArray data = new JSONArray();
        
        for (Product p : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", p.getName());
            obj.put("value", p.getOrder_amount());
            
            data.add(obj);
            
        }
        
        result.put("data", data);
        
        return result.toJSONString();
    }
    
    @RequestMapping("/sugar/api/gmv/spu")
    public String gmvBySpu(
        @RequestParam(value = "date", defaultValue = "0") int date,
        @RequestParam(value = "limit", defaultValue = "5") int limit) { // 如果date不传, 则date=0
        date = defaultDate(date);
        
        List<Product> list = product.gmvBySpu(date, limit);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray columns = new JSONArray();
        
        data.put("columns", columns);
        
        JSONObject c1 = new JSONObject();
        c1.put("name", "spu");
        c1.put("id", "spu_name");
        columns.add(c1);
        
        JSONObject c2 = new JSONObject();
        c2.put("name", "销售额");
        c2.put("id", "oder_amount");
        columns.add(c2);
        
        JSONArray rows = new JSONArray();
        
        for (Product p : list) {
            
            JSONObject row = new JSONObject();
            row.put("spu_name", p.getName());
            row.put("oder_amount", p.getOrder_amount());
            rows.add(row);
            
        }
        data.put("rows", rows);
        
        result.put("data", data);
        
        return result.toJSONString();
    }
    
    @Autowired
    ProvinceStatsService province;
    
    @RequestMapping("/sugar/api/province")
    public String province(@RequestParam(value = "date", defaultValue = "0") int date) { // 如果date不传, 则date=0
        date = defaultDate(date);
        
        List<Map<String, Object>> list = this.province.province(date);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        JSONArray mapData = new JSONArray();
        for (Map<String, Object> map : list) {
            
            JSONObject obj = new JSONObject();
            obj.put("name", map.get("province_name"));
            obj.put("value", map.get("order_amount"));
            
            JSONArray tooltipValues = new JSONArray();
            tooltipValues.add(map.get("order_count"));
            obj.put("tooltipValues", tooltipValues);
            
            mapData.add(obj);
            
        }
        data.put("mapData", mapData);
        
        data.put("valueName", "销售额");
        
        JSONArray tooltipNames = new JSONArray();
        tooltipNames.add("订单数");
        data.put("tooltipNames", tooltipNames);
        
        JSONArray tooltipUnits = new JSONArray();
        tooltipUnits.add("个");
        data.put("tooltipUnits", tooltipUnits);
        
        result.put("data", data);
        
        return result.toJSONString();
    }
    
    @Autowired
    VisitorStatsService visitor;
    
    @RequestMapping("/sugar/api/visitor/hour")
    public String visitorHour(@RequestParam(value = "date", defaultValue = "0") int date) { // 如果date不传, 则date=0
        date = defaultDate(date);
        
        List<Map<String, Object>> list = visitor.statsByHour(date);
        
        //新的 map的key是 hour, value是map自己
        HashMap<Integer, Map<String, Object>> hourToMap = new HashMap<>();
        for (Map<String, Object> map : list) {
            Integer hour = (Integer) map.get("hour");
            hourToMap.put(hour, map);
        }
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        JSONArray categories = new JSONArray();
        for (int i = 0; i < 24; i++) {
            categories.add(i);
        }
        data.put("categories", categories);
        
        JSONArray series = new JSONArray();
        
        JSONObject uv = new JSONObject();
        uv.put("name", "uv");
        JSONArray uvData = new JSONArray();
        uv.put("data", uvData);
        series.add(uv);
        
        JSONObject pv = new JSONObject();
        pv.put("name", "pv");
        JSONArray pvData = new JSONArray();
        pv.put("data", pvData);
        series.add(pv);
        
        JSONObject uj = new JSONObject();
        uj.put("name", "uj");
        JSONArray ujData = new JSONArray();
        uj.put("data", ujData);
        series.add(uj);
        
        for (int hour = 0; hour < 24; hour++) {
            // hour = 0
            Map<String, Object> map = hourToMap.get(hour);
            if (map != null) {
                uvData.add(map.get("uv"));
                pvData.add(map.get("pv"));
                ujData.add(map.get("uj"));
            } else {
                uvData.add(0);
                pvData.add(0);
                ujData.add(0);
            }
        }
        
        data.put("series", series);
        
        result.put("data", data);
        
        return result.toJSONString();
    }
    
    @RequestMapping("/sugar/api/visitor/new_old")
    public String visitorNewOrOld(@RequestParam(value = "date", defaultValue = "0") int date) { // 如果date不传, 则date=0
        date = defaultDate(date);
        
        List<Map<String, Object>> list = visitor.statsByNewOrOld(date);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        data.put("total", list.size());
        
        JSONArray columns = new JSONArray();
        
        JSONObject c1 = new JSONObject();
        c1.put("name", "新老用户");
        c1.put("id", "newOrOld");
        columns.add(c1);
        
        JSONObject c2 = new JSONObject();
        c2.put("name", "独立访客数");
        c2.put("id", "uv");
        columns.add(c2);
        
        JSONObject c3 = new JSONObject();
        c3.put("name", "页面访问数");
        c3.put("id", "pv");
        columns.add(c3);
        
        JSONObject c4 = new JSONObject();
        c4.put("name", "跳出数");
        c4.put("id", "uj");
        columns.add(c4);
        
        data.put("columns", columns);
        
        JSONArray rows = new JSONArray();
        for (Map<String, Object> map : list) {
            
            JSONObject row = new JSONObject();
            row.put("newOrOld", map.get("is_new").equals("0") ? "老用户" : "新用户");
            row.put("uv", map.get("uv"));
            row.put("pv", map.get("pv"));
            row.put("uj", map.get("uj"));
            rows.add(row);
            
        }
        
        data.put("rows", rows);
        
        result.put("data", data);
        
        return result.toJSONString();
    }
    
    @Autowired
    KeywordStatsService kw;
    
    @RequestMapping("/sugar/api/kw")
    public String kw(@RequestParam(value = "date", defaultValue = "0") int date) { // 如果date不传, 则date=0
        date = defaultDate(date);
        
        List<Map<String, Object>> list = kw.statsKeyword(date);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONArray data = new JSONArray();
        for (Map<String, Object> map : list) {
            
            JSONObject obj = new JSONObject();
            obj.put("name", map.get("keyword"));
            obj.put("value", map.get("score"));
            
            data.add(obj);
        }
        result.put("data", data);
        
        return result.toJSONString();
    }
    
    private int defaultDate(int date) {
        if (date == 0) {
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));  // 20210904
        }
        return date;
    }
}
